package net.bplaced.streetfighter.lib.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.preference.PreferenceManager;

/**
 * Utility class to provide some additional functions.
 * 
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public class StreetfighterUtils {

    /**
     * Changes the active content depending on device screen size.
     * 
     * @param manager
     *            FragmentManager to use for fragment replacement
     * @param context
     *            Context to start a new activity
     * @param isTablet
     *            Value indicating whether the device is a tablet or not
     * @param contentId
     *            Id of the fragment to be replaced
     * @param fragment
     *            New fragment to be diplayed
     * @param intent
     *            New intent to be displayed
     */
    public static void changeContent(final FragmentManager manager, final Context context, final boolean isTablet,
            final int contentId, final Fragment fragment, final Intent intent) {

        if (isTablet) {
            manager.beginTransaction().replace(contentId, fragment).commit();

        } else {

            context.startActivity(intent);
        }
    }

    /**
     * Converts a string represented date and time into date format.
     * 
     * @param dateString
     *            String represented date. Format should be yyyy-MM-dd
     * @param timeString
     *            String represented time. Format should be HH:mm:ss
     * @return Date format
     */
    public static Date convertToDate(final String dateString, final String timeString) {

        Date tempDateTime = null;

        try {
            tempDateTime =
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(new StringBuilder(dateString).append(" ")
                            .append(timeString).toString());
        } catch (final ParseException e) {
            e.printStackTrace();
        }

        return tempDateTime;
    }

    /**
     * Retrieves a preference value referenced by a given key.
     * 
     * @param context
     *            Context to access preferences
     * @param prefKey
     *            Key referencing the preference value
     * @param defValue
     *            Default value to return
     * @return Preference value or given default value.
     */
    public static Boolean getPreferenceBoolean(final Activity context, final String prefKey, final boolean defValue) {

        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(prefKey, defValue);
    }

    /**
     * Retrieves a preference value referenced by a given key.
     * 
     * @param context
     *            Context to access preferences
     * @param prefKey
     *            Key referencing the preference value
     * @param defValue
     *            Default value to return
     * @return Preference value or given default value.
     */
    public static Integer getPreferenceInt(final Activity context, final String prefKey, final Integer defValue) {

        return PreferenceManager.getDefaultSharedPreferences(context).getInt(prefKey, defValue);
    }

    /**
     * Retrieves a preference value referenced by a given key.
     * 
     * @param context
     *            Context to access preferences
     * @param prefKey
     *            Key referencing the preference value
     * @param defValue
     *            Default value to return
     * @return Preference value or given default value.
     */
    public static String getPreferenceString(final Activity context, final String prefKey, final String defValue) {

        return PreferenceManager.getDefaultSharedPreferences(context).getString(prefKey, defValue);
    }

    /**
     * Checks whether a specific app is installed or not.
     * 
     * @param activityContext
     *            Context to access package manager
     * @param appIntent
     *            Intent of the app to check
     * @return True or false whether the app is installed or not
     */
    public static boolean isAppInstalled(Context activityContext, Intent appIntent) {

        final List<ResolveInfo> list =
                activityContext.getPackageManager().queryIntentActivities(appIntent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }

    /**
     * Converts the id of the math category into its string representation.
     * 
     * @param category
     *            Id of the category
     * @return String representation
     */
    public static String parseMatchCategory(final Integer category) {

        if (category == 2) {
            return "Heimspiel";
        } else if (category == 3) {
            return "Auswärtsspiel";
        } else if (category == 4) {
            return "Pokalspiel";
        } else if (category == 5) {
            return "PlayOff";
        } else if (category == 6) {
            return "Testspiel";
        } else {
            return "";
        }
    }

    /**
     * Updates or creates a preference value referenced by a given key.
     * 
     * @param context
     *            Context to access preferences
     * @param prefKey
     *            Key referencing the preference value
     * @param value
     *            Value to store
     */
    public static void setPreferenceBoolean(final Activity context, final String prefKey, final Boolean value) {

        final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        final SharedPreferences.Editor prefEditor = sharedPref.edit();
        prefEditor.putBoolean(prefKey, value);
        prefEditor.commit();
    }

    /**
     * Updates or creates a preference value referenced by a given key.
     * 
     * @param context
     *            Context to access preferences
     * @param prefKey
     *            Key referencing the preference value
     * @param value
     *            Value to store
     */
    public static void setPreferenceInt(final Activity context, final String prefKey, final Integer value) {

        final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        final SharedPreferences.Editor prefEditor = sharedPref.edit();
        prefEditor.putInt(prefKey, value);
        prefEditor.commit();
    }

    /**
     * Updates or creates a preference value referenced by a given key.
     * 
     * @param context
     *            Context to access preferences
     * @param prefKey
     *            Key referencing the preference value
     * @param value
     *            Value to store
     */
    public static void setPreferenceString(final Activity context, final String prefKey, final String value) {

        final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        final SharedPreferences.Editor prefEditor = sharedPref.edit();
        prefEditor.putString(prefKey, value);
        prefEditor.commit();
    }
}